public class Num1779 {
    public static void main(String[] args) {
        Num1779 num1779 = new Num1779();
        System.out.println(num1779.nearestValidPoint(3,4,new int[][]{{1,2},{3,1},{2,4},{2,3},{4,4}}));
    }
    public int nearestValidPoint(int x, int y, int[][] points) {
        int n = points.length;
        int minMH = Integer.MAX_VALUE;
        int ans = -1;
        for (int i = 0; i < n; i++) {
            int x1 = points[i][0];
            int y1 = points[i][1];
            if (x1 == x || y1 == y) {
                if (minMH == Integer.MAX_VALUE) {
                    minMH = Math.abs(x - x1) + Math.abs(y - y1);
                    ans = i;
                }else {
                   if (Math.abs(x - x1) + Math.abs(y - y1) < minMH) {
                       minMH = Math.abs(x - x1) + Math.abs(y - y1);
                       ans = i;
                   }
                }
            }
        }
        return  ans;
    }
}
